package com.library.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import java.io.FileInputStream;
import java.security.KeyStore;
@Slf4j
public class HttpRequestUtils {
    public static String refundRequest(String order) throws Exception {
        try {
            KeyStore clientStore = KeyStore.getInstance("PKCS12");
            String machid="1526027941";
            // 读取项目存放的PKCS12证书文件
//            ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
//            Resource resource = resourcePatternResolver.getResource("classpath:apiclient_cert.p12");
//            URL url = resource.getURL();
//            String path = url.getPath();
//            log.info("------------#####################-="+path, path);
            FileInputStream instream = new FileInputStream("/home/api/apiclient_cert.p12");
            try {
                // 指定PKCS12的密码(商户ID)
                clientStore.load(instream, machid.toCharArray());
            } finally {
                instream.close();
            }
            SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(clientStore, machid.toCharArray()).build();
            // 指定TLS版本
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"}, null,
                    SSLConnectionSocketFactory.getDefaultHostnameVerifier());
            // 设置httpclient的SSLSocketFactory
            CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
            try {
                HttpPost httpost = new HttpPost("https://api.mch.weixin.qq.com/secapi/pay/refund"); // 设置响应头信息
//                httpost.addHeader("Connection", "keep-alive");
//                httpost.addHeader("Accept", "*/*");
//                httpost.addHeader("Content-Type", CONTENT_TYPE_FORM.toString());
//                httpost.addHeader("X-Requested-With", "XMLHttpRequest");
//                httpost.addHeader("Cache-Control", "max-age=0");
//                httpost.addHeader("User-Agent", DEFAULT_USER_AGENT);
                httpost.setEntity(new StringEntity(order, "UTF-8"));
                CloseableHttpResponse response = httpclient.execute(httpost);
                try {
                    HttpEntity entity = response.getEntity();
                    String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
                    EntityUtils.consume(entity);
                    return jsonStr;
                } finally {
                    response.close();
                }
            } finally {
                httpclient.close();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
